package beans;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Bullet
{
	int x;
	int y;
	/**
	 * 1.飞行2.爆炸3.待清除
	 */
	public int state;
	
	public Bullet()
	{
	}
	
	public Bullet(int x,int y)
	{
		this.x=x;
		this.y=y;
		flyState();
	}
	
	private void flyState()
	{
		state=1;
	}
	
	private void paintFly(Graphics g)
	{
		Image image = (new ImageIcon("植物大战僵尸/炮弹/子弹.png")).getImage();
		g.drawImage(image, x+84, y+80, null);//子弹绘制的位置
	}
	
	private void flyActivity(ArrayList<Zombie> zombieList)
	{
		x+=5;
		for(  int zombieAmount= 0 ; zombieAmount<= zombieList.size()-1  ; zombieAmount++)//循环遍历所有僵尸
		{
			if(isCollision(zombieList.get(zombieAmount)))//子弹与这个僵尸有碰撞
			{
				bombState();
			}
		}
		
		if(x>=800)//子弹飞出窗口
		{
			clearState();
		}
	}

	private boolean isCollision(Zombie zombie)//子弹与僵尸是否碰撞
	{
		if(new Rectangle(x, y, 30, 30).intersects(zombie.x, zombie.y, 80, 100))
			return true;
		return false;
	}
	
	private void bombState()
	{
		state=2;
	}
	
	private void paintBomb(Graphics g)
	{
		Image image = (new ImageIcon("植物大战僵尸/炮弹/PeaBulletHit.gif")).getImage();
		g.drawImage(image, x+34, y+80, null);//绘制图片API
	}
	
	private void bombActivity(ArrayList<Zombie> zombieList)
	{
		for(int zombieAmount= 0 ; zombieAmount<= zombieList.size()-1 ; zombieAmount++)//循环遍历所有僵尸
		{
			Zombie zombie = zombieList.get(zombieAmount);//指定第i个僵尸
			if(isCollision(zombie))//如果子弹碰撞到了僵尸
			{
				zombie.HP-=100;//这只僵尸减少生命值
				clearState();//子弹移除
			}
		}
	}
	
	private void clearState()
	{
		state=3;
	}
	
	public void activity(ArrayList<Zombie> zombieList)//子弹与僵尸的活动
	{
		if(state==1)//子弹飞行活动
		{
			flyActivity(zombieList);
		}
		else if(state==2)//子弹碰撞活动
		{
			bombActivity(zombieList);
		}
	}
	
	void paint(Graphics g)//画子弹
	{
		if(state==1)//画子弹飞行
		{
			paintFly(g);
		}
		else if(state==2)//画子弹爆炸
		{
			paintBomb(g);
		}
	}
	
	
	
}
